<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Contact extends CI_Controller {

    private $_emailConfig;
    
    public function __construct() {
      parent::__construct();
      
      // Load languge helper
      $this->load->helper('language');
      // Set language
      //if( ! $this->session->userdata('lang') ) // UVEK ES (Spanski)
      {
        $this->session->set_userdata('lang','de');
      }
      // Load language file
      $this->lang->load($this->session->userdata('lang'));

      // Load Favorites library
      $this->load->library('modules');
      
      $this->load->library('email');
  
      // define terms for emails
      $this->_emailConfig['mailtype'] = 'txt';
      $this->_emailConfig['useragent'] = 'PHP CodeIgniter';  
    }
	/**
	 *
	 */
	public function index()
	{       
        $data['title'] = lang('contact_title');
        $data['keywords'] = lang('contact_keywords');
        $data['description'] = lang('contact_description');        
        $data['menu_active'] = "home";
        $data['contents']  = "contact";   
        echo $this->session->userdata('6_letters_code');
		$this->load->view('template/main',$data);
	}
    
    public function send_contact()
    {
      try{
          $contact = $this->input->post('contact');        
             
          $message = sprintf(lang('contact_message'),$contact['name'],$contact['message']);

          $this->email->clear();
          $this->email->initialize($this->_emailConfig);

          $this->email->to($this->config->item('mail_contact'));

          //$this->email->from($user_from->email);
          //$this->email->from($this->config->item('mail_noreply'));
          $this->email->from($contact['email']);
          $this->email->subject(lang('you_have_message_subject'));
          $this->email->message($message);

          if (!$this->email->send()) {
             // Generate error (by CI)
             throw new Exception("Email cannot be sent. Try again or contact admin!");
             //echo $this->email->print_debugger();
             return false;
          }          
          
          $response = array(
                      'ind' => 1,
                      'msg' => lang('contact_message_senden')
                  );
          
        }
        catch (Exception $e) {
            $response = array(
                'ind' => 0,
                'msg' => 'Following errors occured: <br />' . $e->getMessage()
            );
        }
        
        echo json_encode($response);
    }
    
    
} // end class
/* End of file faq.php */
/* Location: ./application/controllers/faq.php */